/**
 * Dear programmer:
 * When I wrote this code, only god and I knew how it worked.
 * Now, only god knows it!
 *
 * @author: Pegasus  Date: 2018-12-15 Time: 23:16
 *
 * 取消清洁工单
 */

import React, {Component} from 'react';
import {
    SafeAreaView,
    View,
    Text,
    TouchableOpacity,
    TextInput, ToastAndroid, ScrollView, Image,
} from 'react-native';
import {connect} from 'react-redux';
import {styles} from './ChangeUseridScreen.Style';
import {
    COLOR_FOCUS,
    COLOR_GRAY_BACKGROUND,
    COLOR_GRAY_LINE,
    COLOR_THEME,
    COLOR_WHITE,
    FONT_COLOR_SUB_TITLE,
    FONT_SIZE_TITLE
} from "../../../../constant/PHColors"
import {changeOrderStatus, changeSweepOrderStatus} from "../../../../component/BottomButtonView/Actions"
import {FetchStatus} from "../../../../configuration/RequestConfiguration";
import {ReturnStatus} from "../../../../configuration/ReturnStatus";
import {doDisplaySuccessMessage, doDisplayWarnMessage} from "../../../../component/Message/Action";
import {retrieveWHSKAlinusers} from "./Actions";
import {SCREEN_WIDTH} from "../../../../constant/PHScreen";
import AntDesign from "react-native-vector-icons/AntDesign";
import User from "../../../../model/User/User";

class ChangeUseridScreen extends Component {

    static navigationOptions = ({navigation}) => ({
        title: (navigation.getParam('titleText')?navigation.getParam('titleText'):''),
        headerRight: (
            <TouchableOpacity activeOpacity={0.9} onPress={()=>{
                navigation.state.params.navigatePress();
            }}>
                <View style={{ height: 44,paddingRight:10, flexDirection: 'row', justifyContent: 'flex-end', alignItems: 'center'}}>
                    <Text style={{fontSize: 16, color: '#212121'}}>
                        确定
                    </Text>
                </View>
            </TouchableOpacity>
        )
    });

    //构造
    constructor(props) {
        super(props);
        this.state = {
            remark:'',
            active:'',
            orderid:this.props.navigation.getParam("orderid"),
            alinusers:[],
            alin_checked:'',
            ordertype:this.props.navigation.getParam("ordertype")
        }
    }



    componentWillReceiveProps(nextProps, nextContext): void {
        let {dispatch} = this.props;

        if (nextProps.onChangeSweepOrderStatus !== this.props.onChangeSweepOrderStatus && nextProps.onChangeSweepOrderStatus.fetchStatus === FetchStatus.Success) {
            if (nextProps.onChangeSweepOrderStatus.RETURN === ReturnStatus.Success) {
                // ToastAndroid.show(nextProps.onChangeSweepOrderStatus.MSG, 1000);//提示
                doDisplaySuccessMessage(nextProps.onChangeSweepOrderStatus.MSG);
                this.props.navigation.navigate(this.props.navigation.getParam("page"));
                this.props.navigation.state.params.goBack();
            } else {
                dispatch(doDisplayWarnMessage(nextProps.onChangeSweepOrderStatus.MSG))
            }
        }

        if (nextProps.onRetrieveWHSKAlinusers !== this.props.onRetrieveWHSKAlinusers && nextProps.onRetrieveWHSKAlinusers.fetchStatus === FetchStatus.Success) {
            if (nextProps.onRetrieveWHSKAlinusers.RETURN === ReturnStatus.Success) {

                this.setState({
                    alinusers:nextProps.onRetrieveWHSKAlinusers.alinusers,
                })
            } else {
                dispatch(doDisplayWarnMessage(nextProps.onRetrieveWHSKAlinusers.MSG))
            }
        }
    }

    componentDidMount(): void {
        let {dispatch} = this.props;

        this.props.navigation.setParams({titleText:this.props.navigation.getParam("text"),navigatePress:this.onPressToDataBack});

        let userid = '';
        User.getUser(user => {
            userid = user.userid;
            this.setState({
                alin_checked:userid
            });
        });


        let str = {"arg":{"opdate":this.state.orderid,"utype":"房务"}}
        dispatch(retrieveWHSKAlinusers(JSON.stringify(str)))
    }

    render(){
        return (
            <SafeAreaView style={styles.body}>
                {/*<View style={styles.bodyTitle}>
                    <Text style={styles.bodyTitleText}>
                        请选择无需清洁原因：
                    </Text>
                </View>*/}
                <ScrollView style={{flex: 1}}>
                    {
                        this.state.alinusers.map((item, index) => {
                            return this._alinusersRender(item, index)
                        })
                    }
                </ScrollView>
            </SafeAreaView>
        )
    }

    _alinusersRender=(item, index)=>{
        return(
            <TouchableOpacity key={index} activeOpacity={0.8} onPress={() => {
                this.setState({
                    alin_checked:item.code,
                })
            }}>
                <View style={{borderBottomWidth: 1,borderColor:COLOR_GRAY_LINE,height: 50,flexDirection: 'row'}}>
                    <View style={{width:50,alignItems: 'center',justifyContent: 'center'}}>
                        {
                            (this.state.alin_checked===item.code)
                                ?
                                (
                                    <AntDesign name={'check'} size={20} style={{color: COLOR_FOCUS,fontWeight: 100}}/>
                                )
                                :
                                (
                                    <View>
                                    </View>
                                )
                        }
                    </View>
                    <View style={{width:SCREEN_WIDTH-50,justifyContent: 'center'}}>
                        <Text style={{fontSize: 18,color:'#555555'}}>
                            {item.name+'-'+item.code}
                        </Text>
                    </View>
                </View>
            </TouchableOpacity>
        )
    }

    onPressToDataBack=()=> {
        let {dispatch} = this.props;



        if(this.state.ordertype==="赶房"||this.state.ordertype==="清洁"){
            let str = {"whsk_Order_Sweep_event":{"orderid":this.state.orderid,},"whsk_Order_event":{"operation":this.props.navigation.getParam("text")}};
            if(this.props.navigation.getParam("text")==="更换查房主管"){
                str.whsk_Order_Sweep_event.iuserid = this.state.alin_checked
            }else if(this.props.navigation.getParam("text")==="更换清洁人"){
                str.whsk_Order_Sweep_event.wuserid = this.state.alin_checked
            }
            dispatch(changeSweepOrderStatus(JSON.stringify(str)));
        }else{
            let str1 = {"whsk_Order_event":{"orderid":this.state.orderid,"operation":this.props.navigation.getParam("text")}}
            str1.whsk_Order_event.wuserid = this.state.alin_checked
            dispatch(changeOrderStatus(JSON.stringify(str1)));
        }
    };

}

const props = state => {
    return {
        onChangeSweepOrderStatus:state.onChangeSweepOrderStatus,
        onRetrieveWHSKAlinusers:state.onRetrieveWHSKAlinusers
    }
};

export default ChangeUseridScreen = connect(props)(ChangeUseridScreen);
